#include "StdAfx.h"
#include "xkRefPoint.h"
#include "DDFun.h"

xkRefPoint::xkRefPoint(void)
{
	m_pImgBit = NULL;

	Initialize();
}

xkRefPoint::~xkRefPoint(void)
{
	UnInitialize();
}

int xkRefPoint::UnInitialize()
{
	m_pImgBit = NULL;

	UnLoadAidList();

	return DD_ERR_SUCCESS;
}


int xkRefPoint::Initialize()
{
	m_xImgBit16.SetImgBit(&m_ImgBitInfo);
	m_xImgBit8.SetImgBit(&m_ImgBitInfo);

	return DD_ERR_SUCCESS;
}

int xkRefPoint::SetImgBuffer(zkImgBitInfo *pImgInfo,BOOL bShareMem)
{
	int nRet = DD_ERR_FAILURE;
	if( NULL == pImgInfo )
	{
		return nRet;
	}

	m_pImgBit = NULL;
	switch( pImgInfo->nImgDefine ) 
	{
	case eImage_Define_Img8:
		{
			m_pImgBit  = &m_xImgBit8;
		}
		break;
	case eImage_Define_Img16:
		{
			m_pImgBit  = &m_xImgBit16;
		}
		break;
	case eImage_Define_Img32:
		{
		}
		break;
	default:
		break;
	}

	do 
	{
		if( NULL == m_pImgBit )
		{
			break;
		}
		nRet = m_pImgBit->SetImgBuffer(pImgInfo,bShareMem);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;;
		}
		nRet = m_pImgBit->CreateKeyPoint();
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

	} while (false);

	return nRet;
}

int xkRefPoint::GetImageSize(int &nWidth,int &nHeight)
{
	int nRet = DD_ERR_FAILURE;
	if( NULL == m_pImgBit )
	{
		return nRet;
	}

	return m_pImgBit->GetImageSize( nWidth,nHeight );
}

int xkRefPoint::GetImgBuffer(zkImgBitInfo **ppImgInfo)
{
    return m_pImgBit->GetImgBuffer( ppImgInfo );
}

int xkRefPoint::GetOrgImgBuffer(zkImgBitInfo **ppImgInfo)
{
	return m_pImgBit->GetOrgImgBuffer( ppImgInfo );
}


int xkRefPoint::SetAnalyCfg(void *pVoid)
{
	if( NULL == pVoid )
	{
		return DD_ERR_PARAMETER;
	}

	m_xAnalyConfig = *( (zkAnalyConfig*)pVoid );

	return 0;
}
int xkRefPoint::GetAnalyCfg(void **ppVoid)
{
	*ppVoid = &m_xAnalyConfig;

	return 0;
}

int xkRefPoint::SetUserData(BYTE *pUserData,int nDataSize)
{
	if( NULL == pUserData || nDataSize <= 0 )
	{
		return DD_ERR_PARAMETER;
	}

	m_xUserData.Allocate( nDataSize );

	memcpy( m_xUserData.pBuffer,pUserData,nDataSize );
	m_xUserData.lLength = nDataSize;

	return 0;
}
int xkRefPoint::GetUserData(BYTE **ppUserData,int &nDataSize)
{
	if( m_xUserData.lLength <= 0 )
	{
		return DD_ERR_FAILURE;
	}

	*ppUserData = m_xUserData.pBuffer;
	nDataSize   = m_xUserData.lLength;

	return 0;
}

int xkRefPoint::LoadFile(char *pFileName)
{
	int nRet = -1;

	m_ImgBitInfo.nImgDefine  = eImage_Define_Img16;
	m_ImgBitInfo.bAutoDelete = FALSE;

	do 
	{
		m_pImgBit  = &m_xImgBit16;
		nRet = m_pImgBit->LoadFile(pFileName);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}
        
        nRet = GetRefDataFromFile(pFileName); 
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		nRet = m_pImgBit->CreateKeyPoint();
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}
          
	} while (false);

	return nRet;
}

int xkRefPoint::SaveFile(char *pFileName)
{
	int nRet = -1;

	do 
	{
		if( NULL == m_pImgBit )
		{
			break;
		}
        nRet = m_pImgBit->SaveFile( pFileName );
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}
		nRet = SaveRefDataToFile( pFileName );
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

	} while (false);

	return nRet;
}



#include "DDReadWriteFile.h"


int xkRefPoint::GetRefDataFromFile(char *pFileName)
{
	int nRet = -1;
	
	DDReadWriteFile xFile;

	do 
	{
		LONGLONG llFileSize = 0;

		if( 0 != xFile.DDCreateFile(pFileName,TRUE,&llFileSize) )
		{
			nRet = -1;
			break;
		}

		LONGLONG llFileOffset = llFileSize - sizeof(zkAnalyFileTail);

		zkAnalyFileTail xFileTail;
		nRet = xFile.DDOperatorFile( (BYTE*)&xFileTail,sizeof(zkAnalyFileTail),&llFileOffset);
		if( nRet != 0)
		{
			break;
		}

		if( !xFileTail.CheckVersion() )
		{
			nRet = -1;
			break;
		}

		DWORD dwRefDataSize = 0;
		//m_xPointInfo
		{
			llFileOffset = xFileTail.dwPointInfoOffset;
			if( xFileTail.dwPointInfoSize != sizeof(m_xPointInfo) )
			{
				nRet = -1;
				break;
			}
			nRet = xFile.DDOperatorFile( (BYTE*)&m_xPointInfo,sizeof(m_xPointInfo),&llFileOffset);
			if( nRet != 0)
			{
				break;
			}
			dwRefDataSize += sizeof(m_xPointInfo);
		}
		//m_xAnalyConfig
		{
			llFileOffset = xFileTail.dwAnalyCfgOffset;
			if( xFileTail.dwAnalyCfgSize != sizeof(m_xAnalyConfig) )
			{
				nRet = -1;
				break;
			}
			nRet = xFile.DDOperatorFile( (BYTE*)&m_xAnalyConfig,sizeof(m_xAnalyConfig),&llFileOffset);
			if( nRet != 0)
			{
				break;
			}
			dwRefDataSize += sizeof(m_xAnalyConfig);
		}
		//m_xUserData
		if( xFileTail.dwUserDataSize > 0 )
		{
			if( !m_xUserData.Allocate( xFileTail.dwUserDataSize ) )
			{
				nRet = DD_ERR_NEWBUFFER;
				break;
			}
			llFileOffset = xFileTail.dwUserDataOffset;
			nRet = xFile.DDOperatorFile( m_xUserData.pBuffer,xFileTail.dwUserDataSize,&llFileOffset);
			if( nRet != 0 )
			{
				break;
			}
			m_xUserData.lLength = xFileTail.dwUserDataSize;
			dwRefDataSize      += xFileTail.dwUserDataSize;
		}
		
		if( dwRefDataSize != xFileTail.dwRefDataSize )
		{
			nRet = -1;
			break;
		}

	} while (FALSE);

	return nRet;
}

int xkRefPoint::SaveRefDataToFile(char *pFileName)
{
	int nRet = -1;

	DDReadWriteFile xFile;

	do 
	{
		LONGLONG llFileSize = 0;

		if( 0 != xFile.DDCreateFile(pFileName,FALSE,&llFileSize) )
		{
			nRet = -1;
			break;
		}
		zkAnalyFileTail xFileTail;
		xFileTail.dwRefDataOffset = (DWORD)llFileSize;

	
		DWORD    dwRefDataSize = 0;
		LONGLONG llFileOffset  = 0;

		llFileOffset = llFileSize;
		//m_xPointInfo
		{
			xFileTail.dwPointInfoOffset = (DWORD)llFileOffset;
			xFileTail.dwPointInfoSize   = sizeof(m_xPointInfo);
			nRet = xFile.DDOperatorFile( (BYTE*)&m_xPointInfo,sizeof(m_xPointInfo),&llFileOffset,FALSE);
			if( nRet != 0)
			{
				break;
			}
			dwRefDataSize += sizeof(m_xPointInfo);
		}
		//m_xAnalyConfig
		{
			xFileTail.dwAnalyCfgOffset = (DWORD)llFileOffset;
			xFileTail.dwAnalyCfgSize   = sizeof(m_xAnalyConfig);
			nRet = xFile.DDOperatorFile( (BYTE*)&m_xAnalyConfig,sizeof(m_xAnalyConfig),&llFileOffset,FALSE);
			if( nRet != 0)
			{
				break;
			}
			dwRefDataSize += sizeof(m_xAnalyConfig);
		}
		//m_xUserData
		if( m_xUserData.lLength > 0 )
		{
			xFileTail.dwUserDataOffset = (DWORD)llFileOffset;
			xFileTail.dwUserDataSize   = m_xUserData.lLength;
	
			nRet = xFile.DDOperatorFile( m_xUserData.pBuffer,xFileTail.dwUserDataSize,&llFileOffset,FALSE);
			if( nRet != 0 )
			{
				break;
			}
			dwRefDataSize += xFileTail.dwUserDataSize;
		}

		xFileTail.dwRefDataOffset = xFileTail.dwPointInfoOffset;
		xFileTail.dwRefDataSize   = dwRefDataSize;

		nRet = xFile.DDOperatorFile( (BYTE*)&xFileTail,sizeof(zkAnalyFileTail),&llFileOffset,FALSE);
		if( nRet != 0)
		{
			break;
		}

	} while (FALSE);

	return nRet;
}




#define DD_ANALYTEMPEXT_AID "aid"

void xkRefPoint::UnLoadAidList()
{
	xkRefAidPoint *pAid = NULL;
	int nCount = (int)m_arRefAidPoint.size();
	for(int i=0;i<nCount;i++)
	{
		pAid = m_arRefAidPoint[i];
		MySafeDelete(pAid);
	}
	m_arRefAidPoint.clear();

	m_xPointInfo.nAidCount = 0;
	m_xPointInfo.pNext     = NULL;
}

int xkRefPoint::LoadAidBuffer(char *pAnalyDir,int nTempType)
{
	UnLoadAidList();

	char szAidTempFile[MAX_PATH] = "";
	char szAidTempPath[MAX_PATH] = "";
	sprintf(szAidTempPath,"%s\\%03d\\%03d\\*.%s",pAnalyDir,nTempType,m_xPointInfo.nPoint,DD_ANALYTEMPEXT_AID);

	int              nRet = DD_ERR_FAILURE;
	int            nCount = 0;
	int            nPoint = -1;
	xkRefAidPoint *pAid   = NULL;

	WIN32_FIND_DATA   filedata; 
	HANDLE            filehandle; 

	filehandle = FindFirstFile( szAidTempPath,&filedata ); 
	if(filehandle!=INVALID_HANDLE_VALUE) 
	{ 
		do 
		{ 
			if( filedata.cFileName ) 
			{ 
				sprintf(szAidTempFile,"%s\\%03d\\%03d\\%s",pAnalyDir,nTempType,m_xPointInfo.nPoint,filedata.cFileName);

				if( NULL == pAid )
				{
					pAid = new xkRefAidPoint();
				}
				nRet = pAid->LoadFile( szAidTempFile );
				if( DD_ERR_SUCCESS == nRet )
				{
					nRet = pAid->CreateHostRelation( m_pImgBit );
					if( DD_ERR_SUCCESS == nRet )
					{
						m_xPointInfo.nAidCount++;
						m_arRefAidPoint.push_back( pAid );
						pAid = NULL;
					}
				}
			} 

		}while(FindNextFile(filehandle,&filedata)); 

		FindClose(filehandle); 
	} 

	MySafeDelete(pAid);

	return 0;
}

int xkRefPoint::AddAidBuffer(char *pAnalyDir,int nTempType,zkImgBitInfo *pImgInfo,BOOL bShareMem)
{
	int nRet = 0;

	xkRefAidPoint *pAid =  new xkRefAidPoint();
    
	do 
	{
		nRet = pAid->SetImgBuffer(pImgInfo,FALSE/*bShareMem*/);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}
		nRet = pAid->CheckAidBuffer();
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		char szAidTempFile[MAX_PATH] = "";
		sprintf(szAidTempFile,"%s\\%03d\\%03d\\%03d.%s",pAnalyDir,nTempType,
			    m_xPointInfo.nPoint,m_xPointInfo.nAidCount,DD_ANALYTEMPEXT_AID);

		DDCreateDeepDir( szAidTempFile );

		nRet = pAid->SaveFile( szAidTempFile );
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		nRet = pAid->CreateHostRelation( m_pImgBit );
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		m_xPointInfo.nAidCount++;
		m_arRefAidPoint.push_back( pAid );
		pAid = NULL;

	} while (false);

	MySafeDelete(pAid);

	return nRet;
}
